package com.moore.utils;

import com.alibaba.fastjson2.JSON;
import com.moore.model.Orders;

import java.util.ArrayList;
import java.util.List;

public class OrderUtils {
    public static String getOrders(String value, String id){
        String sql = "select b.id,b.user_id,a.image,a.name,b.number,b.price,b.state,b.time from goods a, Orders b where ";
        if (id.equals("0")) {
            sql += "a.name like '%" + value + "%' and a.id = b.goods_id";
        }  else {
            sql += "b.user_id = " + id + " and a.id = b.goods_id";
        }
        List<String> list;
        List<String> jsonList = new ArrayList<>();
        try {
            list = JDBCUtils.executeSelect(sql,"id","user_id","image","name","number","price","state","time");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        int i = 0;
        while(true){
            if (8 * i + 7 >= list.size()){
                break;
            }
            Orders orders = new Orders(
                    list.get(8 * i),
                    list.get(8 * i + 1),
                    list.get(8 * i + 2),
                    list.get(8 * i + 3),
                    list.get(8 * i + 4),
                    list.get(8 * i + 5),
                    list.get(8 * i + 6),
                    list.get(8 * i + 7)
            );
            i++;
            jsonList.add(JSON.toJSONString(orders));
        }
        return JSON.toJSONString(jsonList);
    }

    public static String addOrders(String id,String goods_id,String unit_price,String number,String price,String time,String state){
        String sql = "insert into orders (user_id,goods_id,unit_price,number,price,time,state) values (?,?,?,?,?,?,?)";
        int num;
        try {
            num = JDBCUtils.executeUpdate(sql,id,goods_id,unit_price,number,price,time,state);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (num >= 1){
            return "success";
        }else {
            return "fail";
        }
    }

    public static String deleteOrders(String user_id,String id){
        String sql = "delete from orders where user_id = ? and id = ?";
        try {
            int num = JDBCUtils.executeUpdate(sql,user_id,id);
            if (num >= 1){
                return "success";
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "fail";
    }

    public static String updateOrders(String userId, String id,String state){
        String sql = "update orders set state = ? where user_id = ? and id = ?";
        try {
            int num = JDBCUtils.executeUpdate(sql,state,userId,id);
            if (num >= 1){
                return "success";
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "fail";
    }

    public static String getOrdersCount(String id){
        String sql = "select count(*) from orders where user_id = " + id + " and state = 1";
        List<String> list;
        try {
            list = JDBCUtils.executeSelect(sql, "count(*)");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list.get(0);
    }
}
